package cn.com.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.biz.DepartmentsBiz;
import cn.com.entity.Departments;

import com.opensymphony.xwork2.ActionSupport;

public class DepartmentsAction extends ActionSupport implements SessionAware{
	private DepartmentsBiz departmentsbiz;
	private String departmentsmsg;
	private Departments departments;
	private Map<String, Object> session;
	public Map<String, Object> getSession() {
		return session;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		session=arg0;
		
	}
	
	public String getDepartmentsmsg() {
		return departmentsmsg;
	}
	public void setDepartmentsmsg(String departmentsmsg) {
		this.departmentsmsg = departmentsmsg;
	}
	public DepartmentsBiz getDepartmentsbiz() {
		return departmentsbiz;
	}
	public void setDepartmentsbiz(DepartmentsBiz departmentsbiz) {
		this.departmentsbiz = departmentsbiz;
	}
	
	public Departments getDepartments() {
		return departments;
	}
	public void setDepartments(Departments departments) {
		this.departments = departments;
	}
	
	//查询所有科室
	public String selectAllDepartments(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		departmentsbiz=(DepartmentsBiz)ac.getBean("departmentsbiz");
		List<Departments> list=departmentsbiz.selectAlldeDepartments();
		session.put("departmentslist", list);
		return SUCCESS;
	}
	//查询一个科室
	public String selectOneDepartments(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		departmentsbiz=(DepartmentsBiz)ac.getBean("departmentsbiz");
		Departments depart=departmentsbiz.selectOneDepartments(departments);
		session.put("onedepartment", depart);
		return SUCCESS;
	}
	
	//添加科室
	public String addDepartments(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		departmentsbiz=(DepartmentsBiz)ac.getBean("departmentsbiz");
		boolean bool=departmentsbiz.addDepartments(departments);
		if(bool){
			setDepartmentsmsg("添加成功");
			return SUCCESS;
		}else{
			setDepartmentsmsg("添加失败，请重新添加");
			return ERROR;
		}
	}
	
	//更新科室
	public String updateDepartments(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		departmentsbiz=(DepartmentsBiz)ac.getBean("departmentsbiz");
		boolean bool=departmentsbiz.updateDepartments(departments);
		if(bool){
			setDepartmentsmsg("修改成功");
			return SUCCESS;
		}else{
			setDepartmentsmsg("修改失败");
			return ERROR;
		}	
	}
	
	//删除科室
	public String deleteDepartments(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		departmentsbiz=(DepartmentsBiz)ac.getBean("departmentsbiz");
		boolean bool=departmentsbiz.deleteDepartments(departments);
		if(bool){
			setDepartmentsmsg("删除成功");
			return SUCCESS;
		}else{
			setDepartmentsmsg("删除失败");
			return ERROR;
		}	
	}
	
	
}
